/**  
 * 文 件 名: MenuAction.java  
 * 描    述：   
 * 版    权：Copyright (c)20012-2012 tongzhou All Rights Reserved.
 * 公    司: 同舟 
 * 作    者：彭定雄                     
 * 版    本: 1.0  
 * 创建时间: 2012-10-27
 *  
 * 修改历史：  
 * 时间                             作者                       版本                        描述  
 * ------------------------------------------------------------------  
 * 2012-10-27        彭定雄                1.0          1.0 Version  
 */         

package com.tongzhou.system.menu.action;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.tongzhou.db.exception.DBException;
import com.tongzhou.system.menu.dao.IMenuDAO;
import com.tongzhou.system.menu.model.Menu;
import com.tongzhou.system.menu.model.SearchMenuCondition;
import com.tongzhou.system.userMgr.dao.IUserMgrDAO;
import com.tongzhou.system.userMgr.model.User;
import com.tongzhou.util.Util;

public class MenuAction extends ActionSupport{
	private String id;
	private String name;//菜单名称
	private String url;//菜单对应的URL
	private String sort;//菜单排序序号
	private String parent_id;//菜单对应父菜单ID号,0:表示无父菜单
	private String module_id;//模块ID
	private String active;//是否可用(Y：可用N：不可用)
	private String parent_name;
	
	private int pageSize ;
	private int currentPage ;
	private String orderBy ;
	private String orderByType ;
	private List<Menu> result;
	SearchMenuCondition condition;
	private IMenuDAO dao;
	private Menu menu;
	private Menu oldMenu;//修改前菜单
	private Menu beforeMenu;//对应的小一级菜单
	private String oldSort;//修改前序号
	private String oldParent_id;//修改前对应的父菜单id
	private String beforeSort;//修改位于哪个序号下面
	
	private List<Menu> menuList;
	private List<Menu> subMenuList;
	private List<Menu> sortMenuList;
	private String submenuList;
	private String judgment;
	private IUserMgrDAO userDAO;
	
	public String header() throws DBException{
		return "header";
	}
	public String main() throws DBException{
		return "main";
	}
	public String menu() throws DBException{
		User user=new User();
		user.setId(String.valueOf(Util.getAdminUserId()));
//		Role role=userDAO.getRoleByUserID(user);
		Boolean temp=Util.getAdminUserRoles().contains("1");
		if(temp){
			menuList=dao.listMenu();
			result=new ArrayList<Menu>();
			for(Menu menu:menuList){
				subMenuList=dao.getSubMenuList(menu);
				menu.setList(subMenuList);
				result.add(menu);
			}
		}else{
			menuList=dao.listMenu2();
			result=new ArrayList<Menu>();
			for(Menu menu:menuList){
				subMenuList=dao.getSubMenuList2(menu);
				menu.setList(subMenuList);
				result.add(menu);
			}
		}
		return "menu";
	}
	public String bottom() throws DBException{
		return "bottom";
	}
	public String listMenu() throws DBException{
		checkSearchCondition();
		condition = new SearchMenuCondition();
		if (currentPage == 0){
			currentPage = 1;
		}
		condition.setParent_name(parent_name);
		condition.setActive(active);
		condition.setName(name);
		condition.setCurrentPage(currentPage);
		condition.setOrderBy(orderBy);
		condition.setOrderByType(orderByType);
		condition.setPageSize(pageSize) ;
	    result = dao.listMenu(condition);
		return "listMenu";
	}
	/**
	 * 设置列表显示格式
	 *@author: 彭定雄
	 */
	private void checkSearchCondition(){
		if(currentPage <=0){
			currentPage = 1 ;
		}
		
		if(pageSize<=0){
			pageSize = 10 ;
		}
		
		if(orderBy==null||"".equals(orderBy)){
			orderBy = "m.parent_id asc,m.sort" ;
		}
		
		if(orderByType == null || "".equals(orderByType)){
			orderByType = "asc" ;
		}
	}
	
	public String toAddMenu() throws DBException{
		//获取一级菜单
		menu=new Menu();
		menuList=dao.getMenuList(menu);
		return "toAddMenu";
	}
	
	public String checkMenuByName() throws DBException{
		menu=new Menu();
		try {
			menu.setName(new String(name.getBytes("ISO-8859-1"),"UTF-8"));
			menu.setId(id);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		result=dao.checkMenuByName(menu);
		if(result.size()>0){
			judgment="0";
		}else{
			judgment="1";
		}
		return SUCCESS;
	}
	
	public String addMenu() throws DBException{
		menu=new Menu();
		menu.setName(name);
		menu.setUrl(url);
		menu.setParent_id(parent_id);
		menu.setModule_id(module_id);
		menu.setSort(Util.checkObjIsNull(sort)?"0":sort);
		menu.setActive(active);
		dao.addMenu(menu);
		listMenu();
		return "listMenu";
	}
	
	public String getMenuListByParentID() throws DBException{
		menu=new Menu();
		menu.setParent_id(parent_id);
		menuList=dao.getMenuListByParentID(menu);
		
		submenuList="";
		for(Menu m:menuList){
			if(!submenuList.equals("")){
				submenuList+=";";
			}
			submenuList+=m.getSort()+":"+m.getName();
		}
		return SUCCESS;
	}
	
	public String deleteMenu() throws DBException{
		menu=new Menu();
		menu.setId(id);
		
		dao.deleteMenu(menu);
		listMenu();
		return "listMenu";
	}
	
	public String toEditMenu() throws DBException{
		//获取一级菜单
		menu=new Menu();
		menu.setId(id);
		menuList=dao.getMenuList(menu);
		menu=dao.getMenuById(menu);
		beforeMenu=dao.getBeforeMenuById(menu);
		sortMenuList=dao.getSortMenuList(menu);
		return "toEditMenu";
	}
	
	public String editMenu() throws DBException{
		menu=new Menu();
		menu.setId(id);
		menu.setName(name);
		menu.setActive(active);
		menu.setModule_id(module_id);
		menu.setParent_id(parent_id);
		sort=Util.checkObjIsNull(sort)?"0":sort;
		
		if(beforeSort.equals(sort)&&parent_id.equals(oldParent_id)){
			menu.setSort(oldSort);
		}else{
			menu.setSort(String.valueOf(Integer.valueOf(sort)));
		}
//		int i=8/0;
//		menu.setSort(Util.checkObjIsNull(sort)?"1":sort);
		menu.setUrl(url);
		oldMenu=new Menu();
		oldMenu.setSort(oldSort);
		oldMenu.setParent_id(oldParent_id);
		dao.updateMenu(menu,oldMenu);
		listMenu();
		return "listMenu";
	}
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getSort() {
		return sort;
	}
	public void setSort(String sort) {
		this.sort = sort;
	}
	public String getParent_id() {
		return parent_id;
	}
	public void setParent_id(String parentId) {
		parent_id = parentId;
	}
	public String getModule_id() {
		return module_id;
	}
	public void setModule_id(String moduleId) {
		module_id = moduleId;
	}
	public String getActive() {
		return active;
	}
	public void setActive(String active) {
		this.active = active;
	}
	public String getParent_name() {
		return parent_name;
	}
	public void setParent_name(String parentName) {
		parent_name = parentName;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public String getOrderBy() {
		return orderBy;
	}
	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}
	public String getOrderByType() {
		return orderByType;
	}
	public void setOrderByType(String orderByType) {
		this.orderByType = orderByType;
	}
	public List<Menu> getResult() {
		return result;
	}
	public void setResult(List<Menu> result) {
		this.result = result;
	}
	public SearchMenuCondition getCondition() {
		return condition;
	}
	public void setCondition(SearchMenuCondition condition) {
		this.condition = condition;
	}
	public IMenuDAO getDao() {
		return dao;
	}
	public void setDao(IMenuDAO dao) {
		this.dao = dao;
	}
	public Menu getMenu() {
		return menu;
	}
	public void setMenu(Menu menu) {
		this.menu = menu;
	}
	public List<Menu> getMenuList() {
		return menuList;
	}
	public void setMenuList(List<Menu> menuList) {
		this.menuList = menuList;
	}
	public String getSubmenuList() {
		return submenuList;
	}
	public void setSubmenuList(String submenuList) {
		this.submenuList = submenuList;
	}
	public String getJudgment() {
		return judgment;
	}
	public void setJudgment(String judgment) {
		this.judgment = judgment;
	}
	public List<Menu> getSortMenuList() {
		return sortMenuList;
	}
	public void setSortMenuList(List<Menu> sortMenuList) {
		this.sortMenuList = sortMenuList;
	}
	public Menu getOldMenu() {
		return oldMenu;
	}
	public void setOldMenu(Menu oldMenu) {
		this.oldMenu = oldMenu;
	}
	public Menu getBeforeMenu() {
		return beforeMenu;
	}
	public void setBeforeMenu(Menu beforeMenu) {
		this.beforeMenu = beforeMenu;
	}
	public String getOldSort() {
		return oldSort;
	}
	public void setOldSort(String oldSort) {
		this.oldSort = oldSort;
	}
	public String getOldParent_id() {
		return oldParent_id;
	}
	public void setOldParent_id(String oldParentId) {
		oldParent_id = oldParentId;
	}
	public String getBeforeSort() {
		return beforeSort;
	}
	public void setBeforeSort(String beforeSort) {
		this.beforeSort = beforeSort;
	}
	public List<Menu> getSubMenuList() {
		return subMenuList;
	}
	public void setSubMenuList(List<Menu> subMenuList) {
		this.subMenuList = subMenuList;
	}
	public IUserMgrDAO getUserDAO() {
		return userDAO;
	}
	public void setUserDAO(IUserMgrDAO userDAO) {
		this.userDAO = userDAO;
	}
}
